package com.plat.api.util;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: shish
 * Date: 2020/7/6
 * Time: 22:59
 * Description: No Description
 */
@Slf4j
public class CommendUtils {
    // 保存方法
    @ResponseBody
    @RequestMapping(value="/save",produces = "application/json; charset=utf-8")
    public Object save(HttpSession session, HttpServletRequest request,
                       VisitRecord visitRecord, String fileIds, String x, String y){

        ReturnJson json = new ReturnJson();
        Map<String, Object> data = new HashMap<String, Object>();
        ExecutorService executorService = Executors.newCachedThreadPool();
        try {
            UserInfo u = (UserInfo) session.getAttribute(ConstantValue.USER_IN_SESSION);
            Long[] fileIdList = null;
            if(!StringUtils.isEmpty(visitRecord.getVisitName())){
                visitRecord.setPersonnelName(visitRecord.getVisitName());
            }

            if (!org.apache.commons.lang.StringUtils.isEmpty(fileIds)) {
                String[] fileIdStringList = fileIds.split(",");
                fileIdList = (Long[]) ConvertUtils.convert(fileIdStringList,Long.class);
            }

            if (visitRecord.getVisitId() == null || visitRecord.getVisitId() == 0l) {
                visitRecord.setEventStatus("2");
                visitRecord.setOrgId(u.getOrgId());
                String orgCode = request.getParameter("orgCode");
                if (!StringUtils.isEmpty(orgCode)){
                    visitRecord.setOrgCode(orgCode);
                }else{
                    visitRecord.setOrgCode(getGridCode(request));
                }

                if("L".equals(visitRecord.getVisitedType()) && orgCode.startsWith("3607")){
                    if(fileIdList==null || fileIdList.length==0){
                        data.put(Constants.RESULT_CODE, Constants.RESULT_CODE_FAIL);
                        json.setData(data);
                        json.setDesc("图片上传获取数据失败！");
                        return json;
                    }
                    setVisitRecordVistiName(visitRecord);
                }

                Long visitId = visitRecordService.addVisitEventRecordAsync(visitRecord, fileIdList, u);
                if (visitId==null || visitId <= 0l){
                    data.put(Constants.RESULT_CODE, Constants.RESULT_CODE_FAIL);
                    json.setData(data);
                    json.setDesc("走访记录新增失败不成功！");
                    return json;
                }
                visitRecord.setVisitId(visitId);
                if("L".equals(visitRecord.getVisitedType()) && orgCode.startsWith("3607")){//综治的接口，保存赣州的精神病人走访人员
                    try {
                        executorService.execute(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    saveVisitListMember(visitRecord, u, visitId);
                                } catch (Exception e) {
                                    log.error("保存精神病走访人员异常:{}",e.getMessage());
                                }
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if ("003".equals(visitRecord.getRiskAssessment())) {
                    data.put(Constants.RESULT_CODE, Constants.RESULT_CODE_SUCCESS);
                    json.setDesc("保存成功，并将上报事件！");
                } else {
                    data.put(Constants.RESULT_CODE, Constants.RESULT_CODE_SUCCESS);
                    json.setDesc("保存成功");
                }
            }
            json.setData(data);
        } catch (Exception e) {
            log.error("走访异常:{}",e.getMessage());
            data.put(Constants.RESULT_CODE, Constants.RESULT_CODE_FAIL);
            json.setData(data);
            json.setDesc("保存失败，请查看后台日志");
            return json;
        }finally {
            if(executorService!=null){
                executorService.shutdown();
            }
        }
        return json;
    }
}
